<?php
/**
 * User: Hufeng
 * Date: 2017/08/29 19:48
 * Desc: 用户Model
 */
namespace App\Model;
use SasPHP\SqlModel;

class SignDaysModel extends SqlModel{
    protected $dbName = 'signdays';

    public function __construct(){
        parent::__construct('DB_SERVER');
    }

    function insertData($data){
        $this->dbConn->insert($this->dbName,$data);
        return $this->dbConn->id();
    }

    function editData($where,$data){
        $this->dbConn->update($this->dbName,$data,$where);
    }

    function delData($data){
        $this->dbConn->delete($this->dbName, $data);
    }

    function findData($where,$fields='*'){
        $res = $this->dbConn->get($this->dbName,$fields,$where);
        return $res;
    }

    function queryData($where,$fields='*'){
        $res = $this->dbConn->select($this->dbName,$fields,$where);
        return $res;
    }
    //获取统计数据
    public function getTotalNum($where){
        $res = $this->dbConn->count($this->dbName,$where);
        return $res;
    }
    //sum数据
    public function sumData($where,$param){
        $res = $this->dbConn->sum($this->dbName,$param,$where);
        return $res;
    }
    //查询用户连续签到了几天
    function signDays($uid){
        $sdRes = $this->findData(['uid'=>$uid]);
        $ystime = strtotime(date('Y-m-d',strtotime('-1 days')));
        $lastTime = strtotime($sdRes['lasttime']);
        $signDays = $lastTime>=$ystime ? $sdRes['signdays'] : 0;
        return $signDays;
    }
    //更新用户连续签到天数
    function recordDays($uid,$days){
        $sdRes = $this->findData(['uid'=>$uid]);
        if($sdRes){
            $this->editData(['uid'=>$uid],['signdays'=>$days,'lasttime'=>date('Y-m-d H:i:s')]);
        }else{
            $this->insertData(['uid'=>$uid,'signdays'=>$days,'lasttime'=>date('Y-m-d H:i:s')]);
        }
        return;
    }
}
